#include "DistanceTransform.h"
#include "MorphologyErosion.h"



DistanceTransform::DistanceTransform ()
{

}



Ptr< QImage > DistanceTransform::process (Ptr< QImage > pSrcImage)
{
	Ptr< QImage > pDestImage = new QImage (pSrcImage->size (),
			QImage::Format_ARGB32);
	pDestImage->fill (qRgba (0, 0, 0, 255));
	Ptr< QImage > pTempImage;
	MorphologyErosion erosion (Core< int > (QSize (3, 3)));

	int step = 0;
	while (true) {
		pTempImage = erosion.operate (pSrcImage);
		bool hasChange = false;
		for (int v = 0; v < pSrcImage->height (); ++v) {
			QRgb * srcLine = (QRgb *)pSrcImage->scanLine (v);
			QRgb * tempLine = (QRgb *)pTempImage->scanLine (v);
			QRgb * destLine = (QRgb *)pDestImage->scanLine (v);
			for (int u = 0; u < pSrcImage->width (); ++u) {
				if (srcLine[u] != tempLine[u]) {
					destLine[u] = qRgba (step, step, step, 
							qAlpha (srcLine[u]));
					hasChange = true;
				}
			}
		}
		if (!hasChange)
			break;
		pSrcImage = pTempImage;
		++step;
	}
	return pDestImage;
}
